#ifndef MJPGWIDGET2_H
#define MJPGWIDGET2_H

#include <QWidget>
#include <QPixmap>
#include <QPaintEvent>
#include <QByteArray>
#include <QNetworkAccessManager>

class MjpgWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MjpgWidget(QWidget *parent = 0);
    ~MjpgWidget();

public slots:
    void start(const QString &host, quint16 port = 80,
               const QString &uri = "/?action=stream",
               const QString &userName = QString(),
               const QString &passWord = QString());
    void stop();
    void clear();

protected:
    void paintEvent(QPaintEvent *ev);

private:
    QNetworkAccessManager *_nwkManager;
    QNetworkReply *_picReply;
    QString _host;
    quint16 _port;
    QString _userName;
    QString _passWord;
    QString uri;
    QByteArray picData[2];
    int drawIdx, recvIdx;

private slots:
    void onNwkAuthenticationRequired(QNetworkReply *reply,
                                     QAuthenticator *authenticator);
    void onNwkFinished(QNetworkReply *reply);

private:
    void sendRequest();

public:
    void sendGetRequest(const QString &uri);
};

#endif // MJPGWIDGET2_H
